package com.shop.front.dao.user;

import com.shop.domain.user.User;
import com.shop.jpa.BaseDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;

@Repository
public interface UserDao extends BaseDao<User, Long> {

    @Query("from User where mobile = ?1")
    User findUserByMobileOrName(String mobile);

    @Modifying(clearAutomatically = true)
    @Query("update User set cardId = ?2 , realName = ?3 where id = ?1")
    int updateIdCardAndRealName(Long userId, String idCard, String name);

    @Modifying(clearAutomatically = true)
    @Query("update User set pwd = ?2 where id = ?1")
    int modifyPwd(Long adminId, String pwd);

    @Modifying(clearAutomatically = true)
    @Query("update User set balance = balance + ?2 where id = ?1")
    public int addBalance(Long userId, BigDecimal balance);

    @Modifying(clearAutomatically = true)
    @Query("update User set balance = balance - ?2 where id = ?1 and balance - ?2 >= 0")
    public int subtractBalance(Long userId, BigDecimal balance);

    @Modifying(clearAutomatically = true)
    @Query("update User set brokerage = brokerage - ?2 where id = ?1 and brokerage - ?2 >= 0")
    public int subtractBrokerage(Long userId, BigDecimal brokerage);

    @Modifying(clearAutomatically = true)
    @Query("update User set coin = coin - ?2 where id = ?1 and coin - ?2 >= 0")
    public int subtractCoin(Long userId, BigDecimal coin);

    @Modifying(clearAutomatically = true)
    @Query("update User set nickname = ?2 where id = ?1")
    public int updateInfo(Long userId, String nickname);

    @Modifying(clearAutomatically = true)
    @Query("update User set avatar = ?2 where id = ?1")
    public int updateAvatar(Long userId, String avatar);

    @Query(nativeQuery = true, value = "select count(*) from t_user where FIND_IN_SET(?1, spreadids) ")
    int statPromoter(Long userId);

    @Query(nativeQuery = true, value = "select count(*) from t_user t where t.spread_uid=?1 and exists (select 1 from t_auto_pink a where a.uid = t.id) ")
    int queryAllCountFansBySpreadUid(Long userId);

    @Query(nativeQuery = true, value = "select count(*) from t_user t where t.spread_uid=?1 and level >0 ")
    int queryAllCountL1BySpreadUid(Long userId);

    @Modifying(clearAutomatically = true)
    @Query("update User set lastIp = ?2, lastTime = now() where id = ?1")
    int updateLastInfo(Long userId, String ip);

    @Modifying(clearAutomatically = true)
    @Query("update User set spreadids = ?2, ceoId = ?3, spreadTime = ?4 where id = ?1")
    int active(Long userId, String spreadids, Long ceoId, Date spreadTime);


    @Query(nativeQuery = true, value = "select count(*) from t_user where FIND_IN_SET(?1, spreadids)  and level >0 ")
    int queryAllCountL1ToL5(Long userId);

    @Modifying(clearAutomatically = true)
    @Query("update User set account = ?2, mobile = ?2 where id = ?1")
    int modifyMobile(Long userId, String mobile);

    @Modifying(clearAutomatically = true)
    @Query("update User set level = ?2 where id = ?1")
    public int updateUserLevel(Long userId, Long level);


    @Modifying(clearAutomatically = true)
    @Query("update User set integral = integral + ?2 where id = ?1")
    public int addIntegral(Long userId, BigDecimal integral);

    @Modifying(clearAutomatically = true)
    @Query("update User set integral = integral - ?2 where id = ?1 and integral - ?2 >= 0")
    public int subtractIntegral(Long userId, BigDecimal integral);

    @Query("select count(*) from User where  realName = ?1 and cardId = ?2")
    int queryAuthCardIdCount(String realName,String cardId);


    User findUserByMobileAndIsDeleted(String mobile, int isDelete);
}
